package cl.hhha.web.webapp.action;

import com.opensymphony.xwork2.Preparable;
import cl.hhha.web.service.GenericManager;
import cl.hhha.web.model.TipoFormulario;
import cl.hhha.web.webapp.action.BaseAction;

import java.util.List;

public class TipoFormularioAction extends BaseAction implements Preparable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 134047955044772128L;
	private GenericManager<TipoFormulario, Long> tipoFormularioManager;
    private List<TipoFormulario> tipoFormularioes;
    private TipoFormulario tipoFormulario;
    private Long  id;

    public void setTipoFormularioManager(GenericManager<TipoFormulario, Long> tipoFormularioManager) {
        this.tipoFormularioManager = tipoFormularioManager;
    }

    public List<TipoFormulario> getTipoFormularioes() {
        return tipoFormularioes;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String tipoFormularioId = getRequest().getParameter("tipoFormulario.id");
            if (tipoFormularioId != null && !tipoFormularioId.equals("")) {
                tipoFormulario = tipoFormularioManager.get(new Long(tipoFormularioId));
            }
        }
    }

    public String list() {
        tipoFormularioes = tipoFormularioManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public TipoFormulario getTipoFormulario() {
        return tipoFormulario;
    }

    public void setTipoFormulario(TipoFormulario tipoFormulario) {
        this.tipoFormulario = tipoFormulario;
    }

    public String delete() {
        tipoFormularioManager.remove(tipoFormulario.getId());
        saveMessage(getText("tipoFormulario.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            tipoFormulario = tipoFormularioManager.get(id);
        } else {
            tipoFormulario = new TipoFormulario();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (tipoFormulario.getId() == null);

        tipoFormularioManager.save(tipoFormulario);

        String key = (isNew) ? "tipoFormulario.added" : "tipoFormulario.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}